package com.study.mike.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.mike.mapper.KindCatMapper;
import com.study.mike.model.dto.KindCatDTO;
import com.study.mike.model.dto.PageDTO;
import com.study.mike.model.entity.KindCatDO;
import com.study.mike.model.po.CatStatusPO;
import com.study.mike.model.po.FindCatPO;
import com.study.mike.model.po.QueryCatPO;
import com.study.mike.model.po.SaveOrUpKindCatPO;
import com.study.mike.service.KindCatService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author KangHui
 * @Date 2022/11/4 16:10
 */
@Service
public class KindCatServiceImpl extends ServiceImpl<KindCatMapper, KindCatDO> implements KindCatService {
    @Override
    public Boolean saveOrUpdateCat(SaveOrUpKindCatPO po) {
        KindCatDO kindCatDO = new KindCatDO();
        BeanUtils.copyProperties(po, kindCatDO);
        return this.saveOrUpdate(kindCatDO);
    }

    @Override
    public PageDTO<KindCatDTO> queryKindCat(QueryCatPO po) {
        LambdaQueryWrapper<KindCatDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(po.getCatName()), KindCatDO::getCatName, po.getCatName())
                .like(StrUtil.isNotBlank(po.getNickName()), KindCatDO::getNickName, po.getNickName())
                .eq(ObjectUtil.isNotEmpty(po.getGender()), KindCatDO::getGender, po.getGender());
        PageDTO<KindCatDTO> pages = new PageDTO<>(po.getPageCurrent(), po.getPageSize());
        IPage<KindCatDO> result = this.page(pages, wrapper);
        List<KindCatDTO> collect = result.getRecords().stream().map(x -> {
            KindCatDTO kindCatDTO = new KindCatDTO();
            BeanUtils.copyProperties(x, kindCatDTO);
            return kindCatDTO;
        }).collect(Collectors.toList());
        pages.setRecords(collect);
        return pages;
    }

    @Override
    public Boolean deleteKindCat(Long id) {
        return this.removeById(id);
    }

    @Override
    public Boolean setKindCatStatus(CatStatusPO po) {
        KindCatDO kindCatDO = new KindCatDO();
        kindCatDO.setId(po.getId());
        kindCatDO.setStatus(po.getStatus());
        return this.updateById(kindCatDO);
    }

    @Override
    public KindCatDO findCatBy(FindCatPO po) {
        LambdaQueryWrapper<KindCatDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(po.getCatName()), KindCatDO::getCatName, po.getCatName())
                .eq(StrUtil.isNotBlank(po.getNickName()), KindCatDO::getNickName, po.getNickName())
                .eq(StrUtil.isNotBlank(po.getBreed()), KindCatDO::getBreed, po.getBreed())
                .eq(ObjectUtil.isNotEmpty(po.getGender()), KindCatDO::getGender, po.getGender())
                .last("limit 1");
        KindCatDO kindCatDO = this.getOne(wrapper);
        return kindCatDO;
    }
}
